import requests

from django.shortcuts import render
from django.views.generic.base import View
from django.http import HttpResponse
import logging

from .models import CompanyOrg

from .api_list import ApiList
from .http_utils import get_access_token, get_jsapi_ticket, get_user_detail, get_user_id
from utils.json_utils import JsonUtils
from utils.common_enum import ReturnEnum
from utils.response import ResponseData

# Get an instance of a logger
logger = logging.getLogger('django')


class AccessTokenView(View):
    """
    获取钉钉access_token
    """
    def get(self, request):
        token = get_access_token()
        if token:
            response_data = ResponseData(ReturnEnum.SUCCESS.value, '获取token成功！')
        else:
            response_data = ResponseData(ReturnEnum.ERROR.value, '状态token失败！')
        return HttpResponse(JsonUtils.obj_2_json_str(response_data), content_type="application/json")


class JsApiTicketView(View):
    """
    获取jsapi_ticket
    """
    def get(self, request):
        ticket = get_jsapi_ticket('')
        if not ticket:
            token = get_access_token()
            ticket = get_jsapi_ticket(token)
        if ticket:
            response_data = ResponseData(ReturnEnum.SUCCESS.value, '获取token成功！')
        else:
            response_data = ResponseData(ReturnEnum.ERROR.value, '状态token失败！')
        return HttpResponse(JsonUtils.obj_2_json_str(response_data), content_type="application/json")


class UserInfoView(View):
    """
    获取用户信息
    """
    def get(self, request):
        auth_code = request.GET.get('auth_code', '')
        if not auth_code:
            response_data = ResponseData(ReturnEnum.ERROR.value, '授权码为空！')
            return HttpResponse(JsonUtils.obj_2_json_str(response_data), content_type="application/json")
        user_id = get_user_id(auth_code)
        if user_id:
            user_info = get_user_detail(user_id)
            response_data = ResponseData(flag=ReturnEnum.SUCCESS.value, msg='获取用户信息成功！', data=user_info)
            return HttpResponse(JsonUtils.obj_2_json_str(response_data), content_type="application/json")
        else:
            response_data = ResponseData(ReturnEnum.ERROR.value, '获取用户信息失败！')
            return HttpResponse(JsonUtils.obj_2_json_str(response_data), content_type="application/json")


class IndexView(View):
    def get(self, request):
        return render(request, 'dingding/user-info.html', {'zhao': 'zhao.jpg'})


class TestUrlView(View):
    def get(self, request):
        response = requests.get('https://api.github.com/user', auth=('1137829446@qq.com', 'jj19900130'))
        status_code = response.status_code
        return render(request, 'dingding/test-url.html', {'status_code': status_code})


class HelloView(View):
    def get(self, request):
        return render(request, 'dingding/hello.html')


class EchartsDemoView(View):

    def get(self, request):
        return render(request, 'dingding/first_echarts.html')


class ProjectSituationView(View):
    """
    项目情况
    """
    def get(self, request):
        logger.info('ProjectSituationView info log')
        logger.error('ProjectSituationView error log')
        org_list = CompanyOrg.objects.all()
        normal_count = [320, 302, 301, 334, 390, 330]
        abnormal_count = [120, 132, 101, 134, 90, 230]
        return render(request, 'dingding/project_situation.html', {
            'org_list': org_list,
            'normal_count': normal_count,
            'abnormal_count': abnormal_count
        })